package uutalk.netcloud.talk.base.util.view;

import android.app.Application;
import android.support.annotation.StringRes;
import android.view.Gravity;
import android.widget.Toast;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import uutalk.netcloud.talk.base.util.LanguageUtil;

/**
 * Created by hujie on 16/6/15.
 */
public class ToastUtil {

    private static Toast mToast;

    private static Application applicationContext;

    public static void initToast(Application application) {
        applicationContext = application;
    }

    private static Toast createToast() {
        if (mToast == null) {
            mToast = Toast.makeText(applicationContext, "", Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 0, 0);
        }
        return mToast;
    }

    public static void cancleToast() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
    }

    public static void makeToast(CharSequence text) {

        if (text == null || text.length() == 0) {
            return;
        }
        mToast = createToast();
        mToast.setText(text);
        Observable.just(1).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(@NonNull Integer integer) throws Exception {
                mToast.show();
            }
        });

    }

    public static void makeToast(@StringRes int textId) {
        if (textId != 0) {
            makeToast(LanguageUtil.getStringById(textId));
            return;
        }
    }


}
